#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDialogButtonBox>
#include <QPixmap>
#include <QTimer>

#include <QWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    numX = 0;
    numY = 0;
    //numX2 = 260;
   //numY2 = 420;
    truckCounter = 0;
    soldierCounter = 0;
    time = 0;

    gameStarted = false;
    style = "font-weight: bold; font-size: 15px; color: #19FF19; background-color: black; border-style: solid; border: 5px solid #19FF19; border-radius: 5px;";


    ui->setupUi(this);
    ui->gameScreen->hide();
    ui->labelWin->hide();

  //  ui->gameScreen->setGeometry(ui->gameScreen->x() , ui->gameScreen->y(), 950, 500);
    //ui->gameScreen->setStyle(":/images/Starcraft2-logo.jpg");
    //ui->gameScreen->sets
    ui->btnFactory->hide();
    ui->frameResources->hide();
    setMouseTracking(true);
    timer = new QTimer(this);
    timer->setInterval(50);
    connect(timer, SIGNAL(timeout()), this, SLOT(timerHit()));


    SinglePlayer = new QPushButton(this);
    SinglePlayer->setStyleSheet(style);
    SinglePlayer->setText("Singleplayer");
    SinglePlayer->setGeometry(QRect(400,220,159, 49));

    connect(SinglePlayer, SIGNAL(clicked()), this, SLOT(StartSinglePlayer()));

    MultiPlayer = new QPushButton(this);
    MultiPlayer->setStyleSheet(style);
    MultiPlayer->setText("Multyplayer");
    MultiPlayer->setGeometry(QRect(400,272,159, 49));

    connect(MultiPlayer, SIGNAL(clicked()), this, SLOT(StartMultiPlayer()));

    ExitGame = new QPushButton(this);
    ExitGame->setStyleSheet(style);
    ExitGame->setText("Exit Game");
    ExitGame->setGeometry(QRect(790,580,159, 49));

    connect(ExitGame, SIGNAL(clicked()),this, SLOT(exitGame()));
}

MainWindow::~MainWindow()
{
    for(MovableLabel *lbl : objects) {
        lbl->deleteLater();
    }
    delete ui;
}

void MainWindow::timerHit(){

    movementOfWindowUser();

    World::instance().updateEntities();

    for(MovableLabel *lbl : objects){
        if(!lbl->getObject()->isAlive()){
            lbl->hide();
        }
        if(lbl->isClicked()){

            selected = lbl;

            for(MovableLabel *label : objects){
                if(label != lbl){
                    label->unSelect();
                    ui->btnBuildUnit->hide();
                    ui->btnBuildSoldier->hide();
                }

            }
            if(lbl->getObject()->getBase() == true){
                   ui->btnBuildUnit->show();
                   ui->btnBuildSoldier->show();
                   ui->btnBuildTank->show();
            }


            lbl->unClick();

        }

        if(lbl->getObject()->getSelected() == false && lbl->getObject()->getBase() == true && lbl->getObject()->getTeam() == 1){
            ui->btnBuildUnit->hide();
            ui->btnBuildTank->hide();
            ui->btnBuildSoldier->hide();
        }

        //ForMultyplierQString::number(i)
        if(multyPlayerStarted && (lbl->x() != lbl->getObject()->getX()) && (lbl->y() != lbl->getObject()->getY())){
            MultiAction("MOVE+" + QString::number(lbl->getObject()->getID()) + "+" + QString::number(lbl->getObject()->getX()) + "+" + QString::number(lbl->getObject()->getY()) + "+");
        }

        lbl->move(lbl->getObject()->getX(), lbl->getObject()->getY());

        if (truckCounter >= 5 && soldierCounter >= 5){
            ui->gameScreen->hide();
            ui->labelWin->show();
        }
        lbl->updateImgae();

    }
    if(gameStarted){
        time += 50;
        if(time == 1000){
            for(PlayerController* player : World::instance().getPlayers()) {
                player->addResources(2);
            }
            ui->lineResources->setText(QString::number(World::instance().getPlayers().front()->getResources()));
            time = 0;
        }
    }
    if(World::instance().getPlayers().size() > 1){
        /*ComputerController* ai = dynamic_cast<ComputerController*>(World::instance().getPlayerById(1));
        if(ai != NULL){
            ai->update();
        }*/
        World::instance().getPlayerById(0)->update();
    }
}

void MainWindow::startGame()
{
    jugador = currentPlayerName->toPlainText();
    jugador = jugador.trimmed();

    if(jugador != ""){

    playerName->hide();
    currentPlayerName->hide();
    ExitGame->hide();
    StartGame->hide();
    ComputerName->hide();
    currentComputerName->hide();

    gameStarted = true;

    World::instance().addPlayer(1);
    ComputerController *Ai = World::instance().addComputer(2);
    connect(Ai, SIGNAL(createTank(int,int,int)), this, SLOT(bldTank(int,int,int)));

    Entity *newBase = new Entity(0, 0, false, true);
    newBase->setTeam(1);
    MovableLabel *baseLable = new MovableLabel(ui->gameScreen, newBase, "base", "base_sel");
    World::instance().addEntity(newBase);
    baseLable->move(0, 0);
    ui->gameScreen->installEventFilter(baseLable);
    baseLable->show();
    objects.push_back(baseLable);

    Entity *aiBase = new Entity(600,400, false, true);
    aiBase->setTeam(2);
    MovableLabel *aiBaseLable = new MovableLabel(ui->gameScreen, aiBase, "base", "base_sel");
    World::instance().addEntity(aiBase);
    aiBaseLable->show();
    objects.push_back(aiBaseLable);

    ui->btnBuildUnit->hide();
    ui->btnBuildSoldier->hide();
    ui->btnFactory->show();
    ui->frameResources->show();
    ui->gameScreen->show();

    //ui->btnStartGame->hide();
    //ui->btnExit->hide();

    timer->start();
    }
}

void MainWindow::SetPlayerName(){

    SinglePlayer->hide();
    MultiPlayer->hide();

    playerName = new QLabel(this);
    playerName->setStyleSheet(style);
    playerName->setText("Name");
    playerName->setGeometry(QRect(50,80,80, 49));
    playerName->show();

    currentPlayerName = new QTextEdit(this);
    currentPlayerName->setStyleSheet(style);
    currentPlayerName->setGeometry(QRect(150,86,159, 39));
    currentPlayerName->show();
}

void MainWindow::StartSinglePlayer(){

    vector<string> Names4Computers;
    Names4Computers.push_back("Ariega Tribe");
    Names4Computers.push_back("Shihis' Hord");
    Names4Computers.push_back("Panther's Army");
    Names4Computers.push_back("Capis Legion");

    int numName = rand() % 4;
    string compuName = Names4Computers[numName];

    SetPlayerName();

    ComputerName = new QLabel(this);
    ComputerName->setStyleSheet(style);
    ComputerName->setText("Computer");
    ComputerName->setGeometry(QRect(50,131,87, 49));
    ComputerName->show();

    currentComputerName = new QLabel(this);
    currentComputerName->setStyleSheet(style);
    currentComputerName->setText(QString::fromStdString(compuName));
    currentComputerName->setGeometry(QRect(150,134,159, 39));
    currentComputerName->show();

    StartGame = new QPushButton(this);
    StartGame->setStyleSheet(style);
    StartGame->setText("Start Game");
    StartGame->setGeometry(QRect(790,530,159, 49));
    StartGame->show();

    connect(StartGame, SIGNAL(clicked()),this, SLOT(startGame()));

}

void MainWindow::exitGame()
{
    SinglePlayer->deleteLater();
    MultiPlayer->deleteLater();
    ExitGame->deleteLater();
    exit(0);
}
void MainWindow::addtoSelected(QLabel *label){

}
void QWidget::mousePressEvent(QMouseEvent *ev){

}

void MainWindow::on_btnBuildUnit_clicked()
{
    if(World::instance().getPlayers().front()->getResources() >= 30){
        int x = rand() % 551 + 200;
        int y = rand() % 321 + 200;
        Entity *newObject = new Entity(x, y, true, false);
        if(multyPlayerStarted){

            newObject->setTeam(playerPosstion);
        }else{

            newObject->setTeam(1);
        }
        Truck *newObject = new Truck(111, 77);
        newObject->setTeam(1);
        MovableLabel *newLable = new MovableLabel(ui->gameScreen, newObject, "truck", "truck_sel");
        World::instance().addEntity(newObject);
        newLable->move(x, y);
        ui->gameScreen->installEventFilter(newLable);
        newLable->show();
        objects.push_back(newLable);
        truckCounter++;
        World::instance().getPlayers().front()->addResources(-30);
        ui->lineResources->setText(QString::number(World::instance().getPlayers().front()->getResources()));
        if(multyPlayerStarted){
            MultiAction("BUILDCAR+" + QString::number(playerPosstion) + "+" + QString::number(x) + "+" + QString::number(y) + "+");
        }
    }
}

void MainWindow::on_btnBuildSoldier_clicked()
{
    if(World::instance().getPlayers().front()->getResources() >= 15){
        int x = rand() % 451 + 200;
        int y = rand() % 221 + 200;
        Entity *newObject = new Entity(x, y, true, false);
        if(multyPlayerStarted){

            newObject->setTeam(playerPosstion);
        }else{

            newObject->setTeam(1);
        }
        MovableLabel *newLable = new MovableLabel(ui->gameScreen, newObject, "soldiers", "soldiers_sel");
        World::instance().addEntity(newObject);
        newLable->move(x, y);
        ui->gameScreen->installEventFilter(newLable);
        newLable->show();
        objects.push_back(newLable);
        soldierCounter++;
        World::instance().getPlayers().front()->addResources(-15);
        ui->lineResources->setText(QString::number(World::instance().getPlayers().front()->getResources()));
        if(multyPlayerStarted){
            MultiAction("BUILDCACHACO+" + QString::number(playerPosstion) + "+" + QString::number(x) + "+" + QString::number(y) + "+");
        }
    }
}

//HELP ....I CANT DO THIS
void MainWindow::movementOfWindowUser()
{
    p = ui->gameScreen->mapFromGlobal(QCursor::pos());
    int x = p.x();
    int y = p.y();

    int maxX = ui->gameScreen->x(); //9  (10)
    int maxY = ui->gameScreen->y();  //-1 (-10)
    int width = ui->gameScreen->width(); //951  (950)   -------1000      ......new
    int height = ui->gameScreen->height();  //721 (500) ----550        ......new

    if((x > width) && (x < 1000)){  //1000
        numX = numX + 20;
        ui->gameScreen->setGeometry(maxX - 20, maxY, ui->gameScreen->width() + 20, ui->gameScreen->height());
    }

    if((y > height) && (y < 800)){  //550
        numY = numY + 20;
        ui->gameScreen->setGeometry(maxX , maxY - 20, ui->gameScreen->width(), ui->gameScreen->height() + 20);
    }

    if((x < numX) && (x > maxX)){
        numX = numX - 20;
        ui->gameScreen->setGeometry(maxX + 20, maxY, ui->gameScreen->width() - 20, ui->gameScreen->height());
    }

    if((y < numY) && (y > maxY)){
        numY = numY - 20;
        ui->gameScreen->setGeometry(maxX, maxY + 20, ui->gameScreen->width(), ui->gameScreen->height() - 20);
    }


  //  int ofnumy2 = numY;
  //  int ofnumx2 = numX;
  //  int inosdf = 0;

}

/*
void MainWindow::MovementOfWindow2User(){

    p = ui->gameScreen->mapFromGlobal(QCursor::pos());
    int x = p.x();
    int y = p.y();

    int maxX = ui->gameScreen->x(); //9  (10)
    int maxY = ui->gameScreen->y();  //-1 (-10)
    int width = ui->gameScreen->width(); //951  (950)   -------1000      ......new
    int height = ui->gameScreen->height();  //721 (500) ----550        ......new

    if((x > width) && (x < 2000)){  //1000
        numX2 = numX2 + 20;
        ui->gameScreen->setGeometry(maxX - 20, maxY, ui->gameScreen->width() + 20, ui->gameScreen->height());
    }

    if((y > height) && (y < 1100)){  //550
        numY2 = numY2 + 20;
        ui->gameScreen->setGeometry(maxX , maxY - 20, ui->gameScreen->width(), ui->gameScreen->height() + 20);
    }

    if((x < numX2) && (x > maxX)){
        numX2 = numX2 - 20;
        ui->gameScreen->setGeometry(maxX + 20, maxY, ui->gameScreen->width() - 20, ui->gameScreen->height());
    }

    if((y < numY2) && (y > maxY)){
        numY2 = numY2 - 20;
        ui->gameScreen->setGeometry(maxX, maxY + 20, ui->gameScreen->width(), ui->gameScreen->height() - 20);
    }
    int ofnumy2 = numY2;
    int ofnumx2 = numX2;
    int inosdf = 0;

}*/

void MainWindow::on_btnBuildTank_clicked(){
    if(World::instance().getPlayers().front()->getResources() >= 50){
        int x = rand() % 451 + 200;
        int y = rand() % 221 + 200;
void MainWindow::on_btnBuildTank_clicked()
{
    if(World::instance().getPlayers().front()->getResources() >= 2){
        Entity *newObject = new Tank(111, 77);
        if(multyPlayerStarted){

            newObject->setTeam(playerPosstion);
        }else{

            newObject->setTeam(1);
        }
        MovableLabel *newLable = new MovableLabel(ui->gameScreen, newObject, "tank1", "tank1_sel");
        World::instance().addEntity(newObject);
        newLable->move(x, y);
        ui->gameScreen->installEventFilter(newLable);
        newLable->show();
        objects.push_back(newLable);
        soldierCounter++;
        World::instance().getPlayers().front()->addResources(-2);
        ui->lineResources->setText(QString::number(World::instance().getPlayers().front()->getResources()));
        if(multyPlayerStarted){
            MultiAction("BUILDTANK+" + QString::number(playerPosstion) + "+" + QString::number(x) + "+" + QString::number(y) + "+");
        }
    }
}

void MainWindow::bldTank(int team, int Xpos, int Ypos){
    Tank *newObject = new Tank(Xpos, Ypos);
    newObject->setTeam(team);
    MovableLabel *newLable = new MovableLabel(ui->gameScreen, newObject, "tank1", "tank1_sel");
    World::instance().addEntity(newObject);
    newLable->move(rand() % 451 + 200, rand() % 221 + 200);
    ui->gameScreen->installEventFilter(newLable);
    newLable->show();
    objects.push_back(newLable);
}

void MainWindow::StartMultiPlayer()
{
    SetPlayerName();

    JoinGame = new QPushButton(this);
    JoinGame->setStyleSheet(style);
    JoinGame->setText("Join Game");
    JoinGame->setGeometry(QRect(790,476,159, 49));
    JoinGame->show();

    connect(JoinGame, SIGNAL(clicked()), this, SLOT(JoinMultyGame()));

    CreateGame = new QPushButton(this);
    CreateGame->setStyleSheet(style);
    CreateGame->setText("Create Game");
    CreateGame->setGeometry(QRect(790,528,159, 49));
    CreateGame->show();

    connect(CreateGame, SIGNAL(clicked()), this, SLOT(CreateMultyGame()));

    CreatorName = new QTextEdit(this);
    CreatorName->setStyleSheet(style);
    CreatorName->setText("localhost");
    CreatorName->setGeometry(QRect(790,86,159, 39));
    CreatorName->show();

    CreatorHost = new QLabel(this);
    CreatorHost->setStyleSheet(style);
    CreatorHost->setText("Cretor Server");
    CreatorHost->setGeometry(QRect(620,80,159, 49));
    CreatorHost->show();

}

void MainWindow::JoinMultyGame(){ 

    multyPlayerStarted = false;
    hostName = (CreatorName->toPlainText()).trimmed();
    jugador = (currentPlayerName->toPlainText()).trimmed();

    if(hostName != "" && jugador != ""){

        SetChatAbility();

    if (hostName.size() == 0) {
        QMessageBox::critical(this, "Uh oh", "Please specify name of chat server.");
        return;
    }

    thread = new ConnectThread(socket, hostName, jugador, "12345");
    connect(thread, SIGNAL(finished()), this, SLOT(connectFinished()));
    thread->start();
    }
}

void MainWindow::CreateMultyGame(){

    multyPlayerStarted = false;
    jugador = (currentPlayerName->toPlainText()).trimmed();

    if(jugador != ""){

        SetChatAbility();

        StartGame = new QPushButton(this);
        StartGame->setStyleSheet(style);
        StartGame->setText("Start Game");
        StartGame->setGeometry(QRect(790,530,159, 49));
        StartGame->show();

        connect(StartGame, SIGNAL(clicked()),this, SLOT(StartMultiGame()));

    qCommand = "";

    newPlayerConnected = false;
    playerCount = 0;

    connect(&server, SIGNAL(newConnection()), this, SLOT(clientConnected()));
    if (!server.listen(QHostAddress::Any, 5000)) {
        QMessageBox::critical(this, "Uh oh", "Cannot start socket.");
        exit(1);
    }

    //Creates player 1 connection

    hostName = CreatorName->toPlainText();
    if (hostName.size() == 0) {
        QMessageBox::critical(this, "Uh oh", "Please specify name of chat server.");
        return;
    }

    thread = new ConnectThread(socket, hostName, jugador, "12345");
    connect(thread, SIGNAL(finished()), this, SLOT(connectFinished()));
    thread->start();

    connect(StartGame, SIGNAL(clicked()),this, SLOT(StartNetworkGame()));
    }
}

void MainWindow::SetChatAbility()
{

           JoinGame->hide();
           CreateGame->hide();
           CreatorHost->hide();
           CreatorName->hide();

           ChatConnection = new QTextEdit(this);
           ChatConnection->setStyleSheet(style);
           ChatConnection->setGeometry(QRect(20,300,500, 300));
           ChatConnection->show();

           ChatTextPlace = new QTextEdit(this);
           ChatTextPlace->setStyleSheet(style);
           ChatTextPlace->setGeometry(QRect(20,610,500, 39));
           ChatTextPlace->show();

           SendChat = new QPushButton(this);
           SendChat->setStyleSheet(style);
           SendChat->setText("Send");
           SendChat->setGeometry(QRect(522,610,56, 39));
           SendChat->show();

           connect(SendChat, SIGNAL(clicked()), this, SLOT(SendMessages()));
       }

void MainWindow::SendMessages()
{

    Mensage = (ChatTextPlace->toPlainText()).trimmed();
    if(Mensage != ""){
        Mensage = "|" + Mensage;

    MultiAction(Mensage);
    ChatTextPlace->setText("");

    }
}

void MainWindow::MultiCommand(string command){


     string delimiter = "+";
     vector<string> stuff;
     size_t pos = 0;
     string token;
     while ((pos = command.find(delimiter)) != string::npos) {
         token = command.substr(0, pos);
         stuff.push_back(token);
         command.erase(0, pos + delimiter.length());
     }
     if(stuff.size() > 1){
     if(stuff[0] == "START"){
         playerCount4Client = atoi(stuff[1].c_str());
         if(playerPosstion == 2){
             StartMultyOfPlayer2(playerCount4Client);
         }else if(playerPosstion == 3){
             StartMultyOfPlayer3(playerCount4Client);
         }else if(playerPosstion == 4){
             StartMultyOfPlayer4(playerCount4Client);
         }
     }else if(stuff[0] == "MOVE"){

         MoveThroughNetwork(atoi(stuff[1].c_str()), atoi(stuff[2].c_str()), atoi(stuff[3].c_str()));

    }else if(command.find("ATTACK") == 0){

    }else if(stuff[0] == "BUILDCAR"){
         MultyBuildCar(atoi(stuff[1].c_str()), atoi(stuff[2].c_str()), atoi(stuff[3].c_str()));
     }else if(stuff[0] == "BUILDCACHACO"){
         MultyBuildSoldier(atoi(stuff[1].c_str()), atoi(stuff[2].c_str()), atoi(stuff[3].c_str()));
     }else if(stuff[0] == "BUILDTANK"){
         MultyBuildSoldier(atoi(stuff[1].c_str()), atoi(stuff[2].c_str()), atoi(stuff[3].c_str()));
     }
     }
}

void MainWindow::MultiAction(QString comm)
{

    QString str = jugador + ":" + comm + "\n";

    socket->write(str.toStdString().c_str());
}

void MainWindow::GetPlayerGUI(QString connectedPlayers)
{

    connectedPlayers = connectedPlayers.trimmed();

    int hat = connectedPlayers.indexOf('^');
    int players;
    QString playerJoined;

    if(hat >= 0){

        if(GUIDone){
            joinedPlayer.clear();
        }
         players = (connectedPlayers.left(hat)).toInt();
         joinedPlayer.push_back(connectedPlayers.left(hat));
         connectedPlayers = connectedPlayers.mid(hat + 1);

         for(int i = 0; i < players; i++){
             hat = connectedPlayers.indexOf('^');
             playerJoined = connectedPlayers.left(hat);
             joinedPlayer.push_back(playerJoined);
             connectedPlayers = connectedPlayers.mid(hat + 1);
         }

         SetGUIBeforeStart(joinedPlayer);
    }
}

void MainWindow::SetGUIBeforeStart(QVector<QString>& vector)
{

    int numeroDePlayers = (vector[0]).toInt();

    if(!GUIDone){
        playerPosstion = numeroDePlayers;
        GUIDone = true;
    }

    if(numeroDePlayers > 0){

        playerName->setText("Creator");
        playerName->setGeometry(QRect(20,10,87, 39));

        currentPlayerName->setText(vector[1]);
        currentPlayerName->setGeometry(QRect(120,10,120, 39));

        if(numeroDePlayers > 1){
            player2 = new QLabel(this);
            player2->setStyleSheet(style);
            player2->setText("Player 2");
            player2->setGeometry(QRect(20,52,87, 39));
            player2->show();

            player2Name = new QLabel(this);
            player2Name->setStyleSheet(style);
            player2Name->setText(vector[2]);
            player2Name->setGeometry(QRect(120,52,120, 39));
            player2Name->show();

            if(numeroDePlayers > 2){
                player3 = new QLabel(this);
                player3->setStyleSheet(style);
                player3->setText("Player 3");
                player3->setGeometry(QRect(20,94,87, 39));
                player3->show();

                player3Name = new QLabel(this);
                player3Name->setStyleSheet(style);
                player3Name->setText(vector[3]);
                player3Name->setGeometry(QRect(120,94,120, 39));
                player3Name->show();

                if(numeroDePlayers > 3){
                    player4 = new QLabel(this);
                    player4->setStyleSheet(style);
                    player4->setText("Player 3");
                    player4->setGeometry(QRect(20,136,87, 39));
                    player4->show();

                    player4Name = new QLabel(this);
                    player4Name->setStyleSheet(style);
                    player4Name->setText(vector[3]);
                    player4Name->setGeometry(QRect(120,136,120, 39));
                    player4Name->show();
                }
            }
        }
    }
}

// COMMANDS FOR THE MULTIPLAYER SECTION

void MainWindow::StartNetworkGame()
{

    int numeroDeGamer = (joinedPlayer[0]).toInt();
    if(numeroDeGamer > 1){

        MultiAction("START+" + QString::number(playerCount) + "+");
        playerCount4Client = playerCount;
        StartMultyOfPlayer1(playerCount4Client);
    }
}

void MainWindow::StartMultyOfPlayer1(int totalPlayers){
   // ExitGame->hide();
    StartGame->hide();

    gameStarted = true;

    World::instance().addPlayer();
    ComputerController *Ai = World::instance().addComputer();
    connect(Ai, SIGNAL(createTank(int,int,int)), this, SLOT(bldTank(int,int,int)));

    Entity *newBase = new Entity(0, 0, false, true);
    newBase->setTeam(1);
    MovableLabel *baseLable = new MovableLabel(ui->gameScreen, newBase, "base", "base_sel");
    World::instance().addEntity(newBase);
    baseLable->move(0, 0);
    ui->gameScreen->installEventFilter(baseLable);
    baseLable->show();
    objects.push_back(baseLable);

    Entity *aiBase = new Entity(600,0, false, true);
    aiBase->setTeam(2);
    MovableLabel *aiBaseLable = new MovableLabel(ui->gameScreen, aiBase, "base", "base_sel");
    World::instance().addEntity(aiBase);
    aiBaseLable->show();
    objects.push_back(aiBaseLable);

    if(totalPlayers > 2){
        Entity *aiBase2 = new Entity(0,400, false, true);
        aiBase2->setTeam(3);
        MovableLabel *aiBaseLable2 = new MovableLabel(ui->gameScreen, aiBase2, "base", "base_sel");
        World::instance().addEntity(aiBase2);
        aiBaseLable2->show();
        objects.push_back(aiBaseLable2);

        if(totalPlayers > 3){
            Entity *aiBase3 = new Entity(600,400, false, true);
            aiBase3->setTeam(4);
            MovableLabel *aiBaseLable3 = new MovableLabel(ui->gameScreen, aiBase3, "base", "base_sel");
            World::instance().addEntity(aiBase3);
            aiBaseLable3->show();
            objects.push_back(aiBaseLable3);
        }
    }

    ToHideTheServrCreator();
}

void MainWindow::StartMultyOfPlayer2(int totalPlayers){
   // ExitGame->hide();

    gameStarted = true;

    World::instance().addPlayer();
    ComputerController *Ai = World::instance().addComputer();
    connect(Ai, SIGNAL(createTank(int,int,int)), this, SLOT(bldTank(int,int,int)));

    Entity *aiBase = new Entity(0,0, false, true);
    aiBase->setTeam(1);
    MovableLabel *aiBaseLable = new MovableLabel(ui->gameScreen, aiBase, "base", "base_sel");
    World::instance().addEntity(aiBase);
    aiBaseLable->show();
    objects.push_back(aiBaseLable);

    Entity *newBase = new Entity(600, 0, false, true);
    newBase->setTeam(playerPosstion);
    MovableLabel *baseLable = new MovableLabel(ui->gameScreen, newBase, "base", "base_sel");
    World::instance().addEntity(newBase);
    baseLable->move(600, 0);
    ui->gameScreen->installEventFilter(baseLable);
    baseLable->show();
    objects.push_back(baseLable);

    if(totalPlayers > 2){
        Entity *aiBase2 = new Entity(0,400, false, true);
        aiBase2->setTeam(3);
        MovableLabel *aiBaseLable2 = new MovableLabel(ui->gameScreen, aiBase2, "base", "base_sel");
        World::instance().addEntity(aiBase2);
        aiBaseLable2->show();
        objects.push_back(aiBaseLable2);

        if(totalPlayers > 3){
            Entity *aiBase3 = new Entity(600,400, false, true);
            aiBase3->setTeam(4);
            MovableLabel *aiBaseLable3 = new MovableLabel(ui->gameScreen, aiBase3, "base", "base_sel");
            World::instance().addEntity(aiBase3);
            aiBaseLable3->show();
            objects.push_back(aiBaseLable3);
        }
    }

    ToHideTheServrCreator();
}

void MainWindow::StartMultyOfPlayer3(int totalPlayers){
   // ExitGame->hide();

    gameStarted = true;


    World::instance().addPlayer();
    ComputerController *Ai = World::instance().addComputer();
    connect(Ai, SIGNAL(createTank(int,int,int)), this, SLOT(bldTank(int,int,int)));

    Entity *aiBase = new Entity(0,0, false, true);
    aiBase->setTeam(1);
    MovableLabel *aiBaseLable = new MovableLabel(ui->gameScreen, aiBase, "base", "base_sel");
    World::instance().addEntity(aiBase);
    aiBaseLable->show();
    objects.push_back(aiBaseLable);


    Entity *aiBase2 = new Entity(600,0, false, true);
    aiBase2->setTeam(2);
    MovableLabel *aiBaseLable2 = new MovableLabel(ui->gameScreen, aiBase2, "base", "base_sel");
    World::instance().addEntity(aiBase2);
    aiBaseLable2->show();
    objects.push_back(aiBaseLable2);

    if(totalPlayers > 2){
        Entity *newBase = new Entity(0, 400, false, true);
        newBase->setTeam(3);
        MovableLabel *baseLable = new MovableLabel(ui->gameScreen, newBase, "base", "base_sel");
        World::instance().addEntity(newBase);
        baseLable->move(0, 400);
        ui->gameScreen->installEventFilter(baseLable);
        baseLable->show();
        objects.push_back(baseLable);

        if(totalPlayers > 3){
            Entity *aiBase3 = new Entity(600,400, false, true);
            aiBase3->setTeam(4);
            MovableLabel *aiBaseLable3 = new MovableLabel(ui->gameScreen, aiBase3, "base", "base_sel");
            World::instance().addEntity(aiBase3);
            aiBaseLable3->show();
            objects.push_back(aiBaseLable3);
        }
    }

    ToHideTheServrCreator();
}

void MainWindow::StartMultyOfPlayer4(int totalPlayers){
    //ExitGame->hide();

    gameStarted = true;

    player4->hide();
    player4Name->hide();

    World::instance().addPlayer();
    ComputerController *Ai = World::instance().addComputer();
    connect(Ai, SIGNAL(createTank(int,int,int)), this, SLOT(bldTank(int,int,int)));

    Entity *aiBase = new Entity(0,0, false, true);
    aiBase->setTeam(1);
    MovableLabel *aiBaseLable = new MovableLabel(ui->gameScreen, aiBase, "base", "base_sel");
    World::instance().addEntity(aiBase);
    aiBaseLable->show();
    objects.push_back(aiBaseLable);


    Entity *aiBase2 = new Entity(600,0, false, true);
    aiBase2->setTeam(2);
    MovableLabel *aiBaseLable2 = new MovableLabel(ui->gameScreen, aiBase2, "base", "base_sel");
    World::instance().addEntity(aiBase2);
    aiBaseLable2->show();
    objects.push_back(aiBaseLable2);

    if(totalPlayers > 2){
        Entity *aiBase3 = new Entity(0,400, false, true);
        aiBase3->setTeam(3);
        MovableLabel *aiBaseLable3 = new MovableLabel(ui->gameScreen, aiBase3, "base", "base_sel");
        World::instance().addEntity(aiBase3);
        aiBaseLable3->show();
        objects.push_back(aiBaseLable3);

        if(totalPlayers > 3){
            Entity *newBase = new Entity(600, 400, false, true);
            newBase->setTeam(playerPosstion);
            MovableLabel *baseLable = new MovableLabel(ui->gameScreen, newBase, "base", "base_sel");
            World::instance().addEntity(newBase);
            baseLable->move(600, 400);
            ui->gameScreen->installEventFilter(baseLable);
            baseLable->show();
            objects.push_back(baseLable);
        }
    }

    ToHideTheServrCreator();
}

void MainWindow::ToHideTheServrCreator(){

    ExitGame->hide();
    multyPlayerStarted = true;

    playerName->hide();
    currentPlayerName->hide();
    ChatConnection->hide();
    ChatTextPlace->hide();
    SendChat->hide();

    player2->hide();
    player2Name->hide();

    if(playerCount4Client > 2){

            player3->hide();
            player3Name->hide();
        if(playerCount4Client > 3){

            player4->hide();
            player4Name->hide();
        }
    }

    ui->btnBuildUnit->hide();
    ui->btnBuildSoldier->hide();
    ui->btnFactory->show();
    ui->frameResources->show();
    ui->gameScreen->show();

    timer->start();
}

void MainWindow::MoveThroughNetwork(int id, int x, int y){

    for(MovableLabel *lbl : objects){

            selected = lbl;

            for(MovableLabel *label : objects){
                if(label != lbl){
                    label->unSelect();
                    if(label->getObject()->getID() == id){
                        label->move(x, y);
                    }
                }

            }
}
}

void MainWindow::MultyBuildCar(int jugadorr, int x, int y){
    Entity *newObject = new Entity(x, y, true, false);

        newObject->setTeam(jugadorr);

    MovableLabel *newLable = new MovableLabel(ui->gameScreen, newObject, "truck", "truck_sel");
    World::instance().addEntity(newObject);
    newLable->move(x, y);
    ui->gameScreen->installEventFilter(newLable);
    newLable->show();
    objects.push_back(newLable);
    truckCounter++;
}

void MainWindow::MultyBuildSoldier(int jugadorr, int x, int y){
    Entity *newObject = new Entity(x, y, true, false);
    newObject->setTeam(playerPosstion);

        newObject->setTeam(jugadorr);

    MovableLabel *newLable = new MovableLabel(ui->gameScreen, newObject, "soldiers", "soldiers_sel");
    World::instance().addEntity(newObject);
    newLable->move(x, y);
    ui->gameScreen->installEventFilter(newLable);
    newLable->show();
    objects.push_back(newLable);
    soldierCounter++;
}
